package initzation

import (
	"fmt"
	"github.com/go-redis/redis"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"userSrv/global"
	"userSrv/model/entidy"
)

func init() {
	//初始化redis链接
	InitRedis()
	//初始化数据库的链接
	InitRouter()

}

func InitRedis() {
	client := redis.NewClient(&redis.Options{
		Addr:     "175.24.164.107:6379",
		Password: "redis_phhSKN", // no password set
		DB:       0,              // use default DB
	})

	pong, err := client.Ping().Result()
	fmt.Println(pong, err)
	// Output: PONG <nil>
}

func InitRouter() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:mysql_DAJCYW@tcp(175.24.164.107:3306)/week3?charset=utf8mb4&parseTime=True&loc=Local"
	global.Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	global.Db.AutoMigrate(new(entidy.User))
	fmt.Println("mysql数据库初始化成功")
}
